package com.shopping.mall.core.module.cart.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.auth.utils.UserUtils;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.cart.entity.Cart;
import com.shopping.mall.core.module.cart.entity.CartList;
import com.shopping.mall.core.module.cart.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "购物车")
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @ApiOperation("分页查询")
//    @GetMapping("/page")
    public Result<IPage<Cart>> getCartPage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        Cart cart) {
        IPage<Cart> cartPage=cartService.getCartPage(current,size,cart);
        return Result.OK(cartPage);
    }

    @ApiOperation("修改购物车商品（必须传购物车id，可修改数量(number)和稍后购买（laterBuy））")
    @PutMapping
    public Result<CartList> update(@Validated(Cart.update.class) @RequestBody Cart cart) {
        cartService.updateById(cart);
        cart.setUserId(UserUtils.getLoginUserId());
        return Result.OK(cartService.getAll(cart));
    }

    @ApiOperation("新增购物车(goodId,specificationId,number)")
    @PostMapping
    public Result<Object> add(@Validated(Cart.insert.class) @RequestBody Cart cart) {
        return cartService.add(cart);
    }

    @ApiOperation("通过id查询")
//    @GetMapping("/id")
    public Result<Cart> getById(@RequestParam String id){
        Cart cart=cartService.getById(id);
        return Result.OK(cart);
    }

    @ApiOperation("查询购物车（不用传参）")
    @GetMapping
    public Result<CartList> getAll(Cart cart){
        CartList cartList= cartService.getAll(cart);
        return Result.OK(cartList);
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return cartService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return cartService.batchDelete(ids);
    }

}
